package utils

import (
	"errors"
	"fmt"
	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"
	"github.com/go-playground/validator/v10"
	zhTranslations "github.com/go-playground/validator/v10/translations/zh"
	"insighttracking/common/validators"
	"reflect"
	"strings"
)

func Validate(dataStruct interface{}) error {
	zhT := zh.New()
	validate := validator.New()
	// 注册一个函数，获取struct tag里自定义的label作为字段名
	validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
		name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
		if name == "-" {
			return ""
		}
		return name
	})

	uni := ut.New(zhT)
	trans, _ := uni.GetTranslator("zh")
	// 注册自定义结构体字段校验方法
	// 1.注册时间要比当前的晚
	if err := validate.RegisterValidation("checkAfterDate", validators.ValidateAfterDate); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"checkAfterDate",
		trans,
		registerTranslator("checkAfterDate", "{0} 必须要晚于当前日期"),
		translate,
	); err != nil {
		return err
	}
	// 2.自定义手机号码
	if err := validate.RegisterValidation("checkMobile", validators.ValidateMobile); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"checkMobile",
		trans,
		registerTranslator("checkMobile", "{0} 非法的手机号码格式!"),
		translate,
	); err != nil {
		return err
	}
	// 3.自定义email验证
	if err := validate.RegisterValidation("checkEmail", validators.ValidateEmail); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"checkEmail",
		trans,
		registerTranslator("checkEmail", "{0} 非法的邮箱格式!"),
		translate,
	); err != nil {
		return err
	}

	// 4.验证时间格式为2006-01-02
	if err := validate.RegisterValidation("checkDate", validators.ValidateDate); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"checkDate",
		trans,
		registerTranslator("checkDate", "{0} 时间格式必须为2006-01-02!"),
		translate,
	); err != nil {
		fmt.Println("----------------------------")
		return err
	}

	// 5.验证时间格式为2006-01-02 15:04:05
	if err := validate.RegisterValidation("checkDateTime", validators.ValidateDateTime); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"checkDateTime",
		trans,
		registerTranslator("checkDateTime", "{0} 时间格式必须为2006-01-02 15:04:05!"),
		translate,
	); err != nil {
		fmt.Println("++++++++++++++++++++++")
		return err
	}
	// 6.验证金额
	if err := validate.RegisterValidation("decimal", validators.ValidateDecimal); err != nil {
		return err
	}
	if err := validate.RegisterTranslation(
		"decimal",
		trans,
		registerTranslator("decimal", "{0} 格式必须为有效数字!"),
		translate,
	); err != nil {
		return err
	}
	// 验证器注册翻译器
	if err := zhTranslations.RegisterDefaultTranslations(validate, trans); err != nil {
		fmt.Println("000", err)
		return err
	}
	err := validate.Struct(dataStruct)
	if err != nil {
		for _, err1 := range err.(validator.ValidationErrors) {
			return errors.New(err1.Translate(trans))
		}
	}
	return nil
}

// registerTranslator 为自定义字段添加翻译功能
func registerTranslator(tag string, msg string) validator.RegisterTranslationsFunc {
	return func(trans ut.Translator) error {
		if err := trans.Add(tag, msg, false); err != nil {
			return err
		}
		return nil
	}
}

// translate 自定义字段的翻译方法
func translate(trans ut.Translator, fe validator.FieldError) string {
	msg, err := trans.T(fe.Tag(), fe.Field())
	if err != nil {
		panic(fe.(error).Error())
	}
	return msg
}

func RemoveTopStruct(fileds map[string]string) map[string]string {
	rsp := map[string]string{}
	for field, err := range fileds {
		rsp[field[strings.Index(field, ".")+1:]] = err
	}
	return rsp
}
